package com.shanshan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.shanshan.system.entity.Admin;

import com.shanshan.system.entity.Permission;
import com.shanshan.system.mapper.AdminMapper;
import com.shanshan.system.mapper.PermissionMapper;
import com.shanshan.system.service.LoginService;
import com.shanshan.system.util.JwtUtil;
import com.shanshan.system.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Admin)表服务接口
 *
 * @author makejava
 * @since 2023-01-03 09:52:32
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private PermissionMapper permissionDao;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Resource
    private AdminMapper userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getAccount,username);
        Admin myUser = userDao.selectOne(queryWrapper);
        HttpSession session = WebUtil.getSession();
        session.setAttribute("admin",myUser);
        String encode = passwordEncoder.encode(myUser.getPassword());
        myUser.setPassword(encode);
        if(myUser != null){
            List<Permission> permissions = permissionDao.selectAll(myUser.getId());
            /**
             * 返回UserDetails对象---它是一个接口，返回它的实现类。
             * String username,账户
             * String password,密码数据库中的密码
             * 	Collection<? extends GrantedAuthority> authorities: 该用户具有的权限
             */
            List<GrantedAuthority> authorities=
                    permissions.stream()
                            .filter(item-> StringUtils.hasText(item.getCode()))
                            .map(item -> new SimpleGrantedAuthority(item.getCode()))
                            .collect(Collectors.toList());
            User user = new User(username, myUser.getPassword(), authorities);
            System.out.println(user);
            return user;
        }
        return null;
    }

    @Override
    public Admin getInfo() {
        //获取请求token.-----封装工具---可以再任何位置获取request对象和session对象。
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Object username = map.get("username");
        //根据用户名获取整个用户信息.
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("loginname",username);
        return userDao.selectOne(queryWrapper);
    }
}

